package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.DealerRemindExcution;
import com.fortune.dto.in.dealerRemind.DealerRemindConFirmDto;
import com.fortune.dto.in.dealerRemind.DealerRemindListDto;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.DealerRemindEnum;
import com.fortune.service.MDRelationService;
import com.fortune.vo.dealerRemind.DealerRemindList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "dealerRemind", description = "经销商的厂商添加提醒管理", tags = {"经销商的厂商添加提醒管理"})
@RestController
@RequestMapping("dealerRemind")
public class DealerRemindController {
    @Autowired
    private MDRelationService mdRelationService;

    @ApiOperation(value = "提醒列表", nickname = "companyList", notes = "", tags={ "@石匡" })
    @ApiImplicitParams({
            @ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
            @ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
            @ApiImplicitParam(name="type", value="类型(0全部,1已处理,2未处理)",paramType="query",dataType="int",allowableValues = "0,1,2")
    })
    @GetMapping(value = "list",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<PagingData<DealerRemindList>> list(HttpServletRequest request, @ApiIgnore @Validated DealerRemindListDto dealerRemindListDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String dealerId = dUserInfo.getdId();
        dealerRemindListDto.setPage((dealerRemindListDto.getPage()-1)*10);
        List<DealerRemindList> list = mdRelationService.queryCompanyByDealerList(dealerId,dealerRemindListDto);
        int count = mdRelationService.queryCompanyByDealerListCount(dealerId,dealerRemindListDto);
        PagingData<DealerRemindList> pagingData = new PagingData<>();
        pagingData.setData(list);
        pagingData.setRecordsTotal(count);
        return new DealerRemindExcution<PagingData<DealerRemindList>>(pagingData, DealerRemindEnum.SUCCESS);
    }

    @ApiOperation(value = "修改状态", nickname = "mdConfirm", notes = "", tags={ "@石匡" })
    @ApiImplicitParams({
            @ApiImplicitParam(name="id", value="厂商id",paramType="query",dataType="string",required=true),
            @ApiImplicitParam(name="type", value="类型(YES已处理,NO未处理)",paramType="query",dataType="string",required=true,allowableValues = "YES,NO")
    })
    @PutMapping(value = "confirm",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> confirm(HttpServletRequest request,@ApiIgnore @Validated DealerRemindConFirmDto dealerRemindConFirmDto) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String dealerId = dUserInfo.getdId();
        int num = mdRelationService.updateConfirmType(dealerId,dealerRemindConFirmDto);
        if (num == -3){
            return new DealerRemindExcution<Void>(null, DealerRemindEnum.FALSE);
        }else if(num == -2){
            return new DealerRemindExcution<Void>(null, DealerRemindEnum.AGREE);
        }else if(num < 0){
            return new DealerRemindExcution<Void>(null, DealerRemindEnum.IGNORE);
        }
        return new DealerRemindExcution<Void>(null, DealerRemindEnum.SUCCESS);
    }


    @ApiOperation(value = "删除经销商与厂商的联系", nickname = "mdDelete", notes = "", tags={ "@石匡" })
    @ApiImplicitParams({
            @ApiImplicitParam(name="companyId", value="厂商id",paramType="query",dataType="string",required=true)
    })
    @PutMapping(value = "delete",
            produces = { MediaType.APPLICATION_JSON_VALUE })
    public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="用户id不能为空") String companyId) {
        HttpSession session = request.getSession();
        DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
        String dealerId = dUserInfo.getdId();
        int num = mdRelationService.delete(companyId,dealerId);
        if (num < 1){
            return new DealerRemindExcution<Void>(null, DealerRemindEnum.FALSE);
        }
        return new DealerRemindExcution<Void>(null, DealerRemindEnum.SUCCESS);
    }


}
